using System.Collections.Generic;
using System.Reflection;
using System;
namespace com.tc.gateway.gevent
{
    public class EventCenter
    {
        static EventCenter instance;
        static EventCenter()
        {
            instance = new EventCenter();
        }
        static public EventCenter getInstance()
        {
            return instance;
        }
        Dictionary<Type, List<EventListener>> listeners = new Dictionary<Type, List<EventListener>>();

        public void fireEvent(Event e)
        {
            List<EventListener> ls = listeners[e.GetType()];
            foreach (EventListener l in ls)
                l.onEvent(e);
        }
        public void register(EventListener l, Type type)
        {
            List<EventListener> ls = null;
            if(listeners.ContainsKey(type))
                ls = listeners[type];
            if (ls == null)
            {
                ls = new List<EventListener>();
                listeners.Add(type, ls);
            }
            if (ls.Contains(l))
                return;
            ls.Add(l);
        }
        public void remove(EventListener l, Type type)
        {
            List<EventListener> ls = listeners[type];
            ls.Remove(l);

        }
    }
}
